/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland02.model.task.impl;

import dawnland02.data.map.model.MatrixCell;
import dawnland02.data.map.model.MatrixDirection;
import dawnland02.model.action.controller.BuildRoadActionController;
import dawnland02.model.action.controller.RotateActionController;
import dawnland02.model.entity.EntityInternal;
import dawnland02.model.task.BaseTask;
import dawnland02.model.task.GroupTask;
import dawnland02.model.task.TaskStatus;
import dawnland02.service.GeneralService;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
public class BuildRoadTask extends GroupTask{
    public BuildRoadTask(EntityInternal entity) {
        super("BuildRoadTask", entity);
        init();
    }

    @Override
    protected void init(){

        RotateTask rotateTask = new RotateTask(entity, MatrixDirection.UP);
        addTask(rotateTask);

        BaseTask buildRoadTask = new BaseTask("BuildRoad", entity);
        addTask(buildRoadTask);

        setTaskStatus(TaskStatus.INITIALISED);
    }

    @Override
    protected void prepare() {
        RotateTask rotateTask = (RotateTask) getTasks().get(0);
        BaseTask buildRoadTask = (BaseTask) getTasks().get(1);
        MatrixCell matrixCell = entity.getMatrixCell();
        if(matrixCell.getRoadLevel() == 0){
            BuildRoadActionController buildRoadActionController = new BuildRoadActionController(entity, buildRoadTask);
            buildRoadTask.addAction(buildRoadActionController.getAction());
            buildRoadTask.setTaskStatus(TaskStatus.PREPARED);
            setTaskStatus(TaskStatus.PREPARED);
        } else {
            rotateTask.setTaskStatus(TaskStatus.PREPARED_AND_ACTION_NOT_REQUIRED);
            buildRoadTask.setTaskStatus(TaskStatus.PREPARED_AND_ACTION_NOT_REQUIRED);
            setTaskStatus(TaskStatus.PREPARED_AND_ACTION_NOT_REQUIRED);
        }
    }
}
